package com.example.pcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pcl.exception.ObjectException;
import com.example.pcl.pojo.dto.AnnouncementQueryDTO;
import com.example.pcl.pojo.dto.AnnouncementRequestDTO;
import com.example.pcl.pojo.entity.Announcements;
import com.example.pcl.mapper.AnnouncementsMapper;
import com.example.pcl.pojo.vo.AnnouncementResponseVO;
import com.example.pcl.result.PageResult;
import com.example.pcl.service.IAnnouncementsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 公告表，系统公告或重要信息发布 服务实现类
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@Service
@Slf4j
public class AnnouncementsServiceImpl extends ServiceImpl<AnnouncementsMapper, Announcements> implements IAnnouncementsService {
    @Override
    public void addAnnouncement(AnnouncementRequestDTO requestDTO) {
        Announcements announcement = new Announcements();
        BeanUtils.copyProperties(requestDTO, announcement);
        boolean saved = save(announcement);
        if (!saved) {
            throw new ObjectException("添加公告失败！");
        }
    }

    @Override
    public void updateAnnouncement(Integer id, AnnouncementRequestDTO requestDTO) {
        Announcements announcement = getById(id);
        if (announcement == null) {
            throw new ObjectException("公告不存在！");
        }
        BeanUtil.copyProperties(requestDTO, announcement);
        boolean updated = updateById(announcement);
        if (!updated) {
            throw new ObjectException("更新公告失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAnnouncements(List<Integer> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ObjectException("公告ID列表不能为空！");
        }
        boolean removed = removeBatchByIds(ids.stream().distinct().toList());
        if (!removed) {
            throw new ObjectException("删除公告失败！");
        }
    }

    @Override
    public PageResult<?> pageList(@NotNull AnnouncementQueryDTO queryDTO) {
        // 创建查询条件
        LambdaQueryWrapper<Announcements> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(queryDTO.getDisplayFlag()), Announcements::getDisplayFlag, queryDTO.getDisplayFlag());
        queryWrapper.like(StringUtils.isNotBlank(queryDTO.getTitle()), Announcements::getTitle, queryDTO.getTitle());
        queryWrapper.ge(queryDTO.getStartTime() != null, Announcements::getCreatedAt, queryDTO.getStartTime());
        queryWrapper.le(queryDTO.getEndTime() != null, Announcements::getCreatedAt, queryDTO.getEndTime());
        queryWrapper.orderByDesc(Announcements::getCreatedAt);

        // 分页查询
        Page<Announcements> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        Page<Announcements> result = page(page, queryWrapper);

        log.info("{}", result.getRecords());

        // 转换为响应数据列表
        List<AnnouncementResponseVO> items = result.getRecords().stream().map(announcement -> {
            AnnouncementResponseVO responseDTO = new AnnouncementResponseVO();
            BeanUtils.copyProperties(announcement, responseDTO);
            return responseDTO;
        }).toList();

        // 返回自定义分页结果
        return new PageResult<>(result.getTotal(), items);
    }


    @Override
    public AnnouncementResponseVO getAnnouncementById(Integer id) {
        Announcements announcement = getById(id);
        if (announcement == null) {
            throw new ObjectException("公告不存在！");
        }
        AnnouncementResponseVO responseDTO = new AnnouncementResponseVO();
        BeanUtils.copyProperties(announcement, responseDTO);
        return responseDTO;
    }
}
